package com.woniuxy.mapper;

import com.woniuxy.entity.Perms;
import com.woniuxy.entity.Role;
import com.woniuxy.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select uid,account,pwd from t_user where account = #{account}")
    public User findUserByAccount(String account);

    //下面三个sql语句联合查询
    @Select("select uid from t_user where account = #{account}")
    @Results({
        @Result(id = true,column = "uid",property = "uid"),
            //看entity中的属性
        @Result(column = "uid",property = "roles",many = @Many(select = "findRolesByUid",fetchType = FetchType.LAZY))
    })
    public User findRolesAndPerms(String account);

    @Select("select * from t_user_role ur,t_role r where ur.rid=r.rid and ur.uid=#{uid}")
    @Results({
            @Result(id=true,column = "rid",property = "rid"),
            @Result(column = "rid",property = "perms",many = @Many(select = "findPermsByRid"))
    })
    public List<Role> findRolesByUid(int uid);

    @Select("select * from t_role_perms rp,t_perms p where rp.pid=p.pid and rp.rid=#{rid}")
    public List<Perms> findPermsByRid(int rid);

}
